package com.kun.controller.admin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.kun.controller.BaseController;
import com.kun.entity.Role;
import com.kun.entity.User;
import com.kun.result.ExceptionMsg;
import com.kun.result.Response;
import com.kun.service.RolePermissionService;
import com.kun.service.UserService;

/**
 * @description: 
 * @author: KM
 * 2018年5月9日 下午4:34:48
 */
@Controller
@RequiresAuthentication
@RequestMapping("/admin")
public class UserAdminController extends BaseController{
	
	@Autowired
	private UserService  userServiceImpl;
	
	@Autowired
	private RolePermissionService rolePermissionService;
	
	//用户列表页面
	@RequiresPermissions("userInfo:view")
	@RequestMapping("/userList")
	public String userList(){
		return "admin/userList";
	}
	
	//用户认证审批页面
	@RequiresPermissions("userAuth:view")
	@RequestMapping("/authUser")
	public String authUser(){
		return "admin/authUser";
	}
	
	//用户押金共享页面
	@RequiresPermissions("depositOrShare:view")
	@RequestMapping("/depositOrShareAuth")
	public String depositOrShareAuth(){
		return "admin/depositOrShareAuth";
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		//转换日期
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
	}
	
	//用户列表
	@RequiresPermissions(value={"userInfo:view","depositOrShare:view"},logical=Logical.OR)
	@RequestMapping("/getUser")
	@ResponseBody
	public Map<String,Object> getUser(@RequestParam(name = "begin") int begin,
			@RequestParam(name = "length") int length,String jsonString){
		Map<String,Object> map = userServiceImpl.selectAllUser(begin, length, jsonString,false);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("count",map.get("allRecord"));
		m.put("data",map.get("userList"));
		m.put("code", 0);
		m.put("msg", "");
		return m;
	}
		
	//用户待审批列表
	@RequiresPermissions("userAuth:view")
	@RequestMapping("/getAuthUser")
	@ResponseBody
	public Map<String,Object> getAuthUser(@RequestParam(name = "begin") int begin,
			@RequestParam(name = "length") int length,String jsonString){
		Map<String,Object> map = userServiceImpl.selectAllUser(begin, length, jsonString,true);
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("count",map.get("allRecord"));
		m.put("data",map.get("userList"));
		m.put("code", 0);
		m.put("msg", "");
		return m;
	}
	
	//更新用户
	@RequiresPermissions("userInfo:update")
	@RequestMapping(value="updateUser",method=RequestMethod.POST) 
	@ResponseBody
    public Response updateUser(User user){
		int i = userServiceImpl.updateUserByIdSelective(user);
		if(i>0){
			return result(ExceptionMsg.SUCCESS);
		}
		return result(ExceptionMsg.FAILED);
	}
	
	//实名认证 赋予角色
	@RequiresPermissions("userInfo:update")
	@RequestMapping(value="authUser",method=RequestMethod.POST) 
	@ResponseBody
    public Response authUser(User user){
		int i = userServiceImpl.updateUserByIdSelective(user);
		if(i>0){
			//添加角色
			rolePermissionService.insertUserRole(user.getId(), 4);
			return result(ExceptionMsg.SUCCESS);
		}
		return result(ExceptionMsg.FAILED);
	}
	
	//实名认证 赋予角色
	@RequiresPermissions("userInfo:update")
	@RequestMapping(value="deposit",method=RequestMethod.POST) 
	@ResponseBody
    public Response deposit(User user){
		int i = userServiceImpl.updateUserByIdSelective(user);
		int r = 1;
		if(i>0){
			int isHave = 0;
			//判断用户当前是否存在押金会员的角色
			List<Role> roleList = rolePermissionService.selectRoleAndPermissionByUserId(user.getId());
			for (Role role : roleList) {
				if("deposit_vip".equals(role.getRoleName())){
					isHave = 1;
				}
			}
			if(user.getDeposit()>0){//押金大于0
				if(isHave==0){
					//添加 押金会员角色
					r = rolePermissionService.insertUserRole(user.getId(), 6);
				}
			}else{
				if(isHave==1){
					r = rolePermissionService.deleteUserRole(user.getId(), 6);
				}
			}
			if(r>0){
				return result(ExceptionMsg.SUCCESS);
			}
		}
		return result(ExceptionMsg.FAILED);
	}

}
